/*
 * Copyright 2010 Yaroslav Klymko aka t3hnar
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ua.t3hnar.io.serializer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author Yaroslav Klymko aka t3hnar
 */
class StreamOwnerMock implements SerializableFieldOwner {
// ------------------------------ FIELDS ------------------------------

    private static final Log LOG = LogFactory.getLog(StreamOwnerMock.class);
    private static final long serialVersionUID = 3941064048520313000L;

    private String name;

    @SerializableField
    private transient InputStream inputStream0, inputStream1;

// --------------------- GETTER / SETTER METHODS ---------------------

    public InputStream getInputStream0() {
        return this.inputStream0;
    }

    public void setInputStream0(final InputStream inputStream0) {
        this.inputStream0 = inputStream0;
    }

    public InputStream getInputStream1() {
        return this.inputStream1;
    }

    public void setInputStream1(final InputStream inputStream1) {
        this.inputStream1 = inputStream1;
    }

    public String getName() {
        return this.name;
    }

    public void setName(final String name) {
        this.name = name;
    }

// -------------------------- OTHER METHODS --------------------------

    private void readObject(final ObjectInputStream input) throws IOException, ClassNotFoundException {
        input.defaultReadObject();
        try {
            readStreamOwner(input);
        } catch (StreamSerializationException e) {
            LOG.error(e, e);
        }
    }

    @Override
    public void readStreamOwner(final ObjectInputStream input) throws StreamSerializationException {
        Serializer.getInstance(this).deserializeStreams(input);
    }

    private void writeObject(final ObjectOutputStream output)
            throws IOException {
        output.defaultWriteObject();
        try {
            writeStreamOwner(output);
        } catch (StreamSerializationException e) {
            LOG.error(e, e);
        }
    }

    @Override
    public void writeStreamOwner(final ObjectOutputStream output) throws StreamSerializationException {
        Serializer.getInstance(this).serializeStreams(output);
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        final StreamOwnerMock that = (StreamOwnerMock) o;

        return !(this.name != null ? !name.equals(that.name) : that.name != null);
    }

    @Override
    public int hashCode() {
        return this.name == null ? 0 : name.hashCode();
    }
}
